import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { forkJoin } from 'rxjs';

@Component({
  selector: 'app-forkjoin',
  templateUrl: './forkjoin.component.html',
  styleUrls: ['./forkjoin.component.less']
})
export class ForkjoinComponent implements OnInit {

  constructor(private http: HttpClient) { }
  
  ngOnInit() {
    // 并联请求
    const post1 = this.requestData1();
    const post2 = this.requestData2();
    forkJoin([post1, post2])
      .subscribe((data: any) => {
        const postResult1 = data[0]; // '/api/post1的返回结果'
        const postResult2 = data[1]; // '/api/post2的返回结果'
      });
  }
  // 并联请求1
  requestData1() {
    return this.http.get('/api/post1')
      .pipe((data) => {
        return data;
      });
  }


  // 并联请求2
  requestData2() {
    return this.http.get('/api/post2')
      .pipe((data) => {
        return data;
      });
  }
}
